import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

EasyRefreshController useEasyRefreshController({
  bool controlFinishRefresh = false,
  bool controlFinishLoad = false
}) {
  return use(_EasyRefreshControllerHook(controlFinishRefresh, controlFinishLoad));
}

class _EasyRefreshControllerHook extends Hook<EasyRefreshController> {
  const _EasyRefreshControllerHook(this.controlFinishRefresh, this.controlFinishLoad);

  final bool controlFinishRefresh;
  final bool controlFinishLoad;

  @override
  HookState<EasyRefreshController, Hook<EasyRefreshController>> createState() {
    return _EasyRefreshControllerHookState();
  }
}

class _EasyRefreshControllerHookState extends HookState<EasyRefreshController, _EasyRefreshControllerHook> {
  late EasyRefreshController _controller;

  @override
  void initHook() {
    super.initHook();
    _controller = EasyRefreshController(
      controlFinishRefresh: hook.controlFinishRefresh,
      controlFinishLoad: hook.controlFinishLoad
    );
  }

  @override
  EasyRefreshController build(BuildContext context) {
    return _controller;
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}
